package br.ufrj.ppgi.infovis.mtgraph;

public class GraphEdgeManager {
	public static GraphEdge makeGraphEdge(GraphNode a, GraphNode b) throws GraphException {
		
		if (a.equals(b)) {
			throw new GraphException("Trying to connect a node to itself");
		}
		
		AdjacentEntry e = a.getAdjacent(b);
		
		if (e != null) {
			return e.getEdge();
		}
		
		GraphEdge newEdge = new GraphEdge(a, b);
		a.addAdjacent(new AdjacentEntry(b, newEdge));
		b.addAdjacent(new AdjacentEntry(a, newEdge));
		
		newEdge.setComponent();
		newEdge.updateLines();
		
		a.getGraph().addEdge(newEdge);
		
		return newEdge;
	}
	
	public static void removeGraphEdge(GraphEdge edge) {
		GraphNode [] nodes = edge.getNodes();
		GraphNode a = nodes[0];
		GraphNode b = nodes[1];
		
		a.removeAdjacent(b);
		b.removeAdjacent(a);
		
		a.getGraph().removeEdge(edge);
		
	}
}
